<?php
namespace App\Controller\Admin;

use App\Query\OrderQuery;
use App\Query\UserQuery;
use App\Query\UserLogQuery;
use App\Query\UserPayQuery;

class TotalController extends Controller
{
    // 用户统计
    public function user()
    {
        // 时间条件
        $where = $this->inputs('time');
        
        // 基础方法
        $function = function () use($where)
        {
            return UserQuery::query(UserQuery::model(), $where);
        };
        
        return [
            // 会员总数
            'user' => $function()->count(),
            
            // 未用余额
            'money' => $function()->sum('money'),
            
            // 未使用优惠
            'money_discount' => $function()->sum('money_discount'),
            
            // 钱包池
            'money_wallet'   => $function()->sum('money_wallet'),
            
            // 钱包分红
            'money_wallet_red' => $function()->sum('money_wallet_red')
        ];
    }
    
    // 报单
    public function userPay()
    {
        // 时间条件
        $where = $this->inputs('time');
        
        // 基础方法
        $function = function () use($where)
        {
            return UserPayQuery::statusOk(
                UserPayQuery::query(UserPayQuery::model(), $where)
            );
        };
        
        return [
            // 订单总数
            'total' => $function()->count(),
            
            // 订单金额
            'money' => $function()->sum('money'),
            
            // 订单使用优惠
            'discount' => $function()->sum('discount'),
        ];
    }
    
    // 奖金
    public function userLog()
    {
        // 时间条件
        $where = $this->inputs('time', 'type');
        
        // 基础方法
        $function = function () use($where)
        {
            return UserLogQuery::query(UserLogQuery::model(), $where);
        };
        
        return [
            // 未用余额
            'money' => $function()->sum('money'),
        ];
    }
    
    // 订单商品统计
    public function orderGoods()
    {
        // 时间条件
        $where = $this->inputs('time');
        
        // 基础方法
        $function = function () use($where)
        {
            return OrderQuery::query(OrderQuery::model(), $where);
        };
        
        return [
            'data' => $function()->groupBy('product_id')->selectRaw('product_name, sum(money) as money, sum(count) as count, count(*) as total')->get()
        ];
    }
}
